import pygtk
pygtk.require('2.0')
import gtk
import ConfigParser


class AddServerWindow(gtk.Window):
    def __init__(self, serverdb, keyring):
        super(AddServerWindow, self).__init__()
        self.server = "pyrdp"
        self.set_title("pyRDP - Add Server")
        self.set_size_request(400, 150)
        self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("grey"))
        self.set_position(gtk.WIN_POS_CENTER)
        self.serverdb = serverdb
        self.keyring = keyring
        self.config = ConfigParser.ConfigParser()
        self.config.read('etc/pyrdp.conf')
        self.set_resizable(False)
        vbox = gtk.VBox(False, 2)

        table = gtk.Table(4, 2, False)
        table.set_col_spacings(0)
        self.hostname_label = gtk.Label("Server:")
        self.hostname_entry = gtk.Entry()
        self.cname_label = gtk.Label("Shortname:")
        self.cname_entry = gtk.Entry()

        self.user_label = gtk.Label("User:")
        self.user_cbox = gtk.combo_box_new_text()
        add_button_fixed = gtk.Fixed()

        add_button = gtk.Button("Add")
        add_button.set_size_request(50,35)
        add_button_fixed.put(add_button,50,35)
        add_button.connect("clicked", self.on_click, self)
        for user in keyring.get_credentials_list(self.server):
            self.user_cbox.append_text(user.attributes["user"])
        f = gtk.Fixed()
        f.put(self.user_cbox, 0, 0)
        self.user_cbox.set_active(0)

        table.attach(self.hostname_label, 0, 1, 0, 1, gtk.FILL)
        table.attach(self.hostname_entry, 1, 2, 0, 1, gtk.FILL)
        table.attach(self.cname_label, 0, 1, 1, 2, gtk.FILL)
        table.attach(self.cname_entry, 1, 2, 1, 2, gtk.FILL)
        table.attach(self.user_label, 0, 1, 2, 3, gtk.FILL)
        table.attach(f, 1, 2, 2, 3, gtk.FILL)
        table.attach(add_button_fixed, 0,1,3,4, gtk.FILL)
        vbox.pack_start(table, True, True, 0)
        
        self.add(vbox)
        
        self.show_all()
    
    def on_click(self, widget, test):
        self.serverdb.add_server(self.cname_entry.get_text(), self.hostname_entry.get_text(), self.keyring.get_credentials(self.server, self.user_cbox.get_active_text()).attributes["user"])
        self.serverdb.write_file(self.config.get("pyrdp", "serverdb"))
        self.destroy()

class EditServerWindow(gtk.Window):
    def __init__(self, serverdb, keyring):
        super(EditServerWindow, self).__init__()
        self.server = "pyrdp"
        self.set_title("pyRDP - Edit Servers")
        self.set_size_request(600, 400)
        self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("grey"))
        self.set_position(gtk.WIN_POS_CENTER)
        #self.connect("destroy", gtk.main_quit)
        self.set_resizable(False)
        self.serverdb = serverdb
        self.keyring = keyring
        self.config = ConfigParser.ConfigParser()
        self.config.read('etc/pyrdp.conf')
        vbox = gtk.VBox(False, 8)
        
        sw = gtk.ScrolledWindow()
        sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        
        vbox.pack_start(sw, True, True, 0)

        store = self.create_model()

        treeView = gtk.TreeView(store)
        treeView.connect("row-activated", self.on_activated)
        treeView.set_rules_hint(True)
        sw.add(treeView)

        table = gtk.Table(3, 2, False)
        table.set_col_spacings(0)
        self.hostname_label = gtk.Label("Server:")
        self.hostname_entry = gtk.Entry()
        self.cname_label = gtk.Label("Shortname:")
        self.cname_entry = gtk.Entry()
        self.user_label = gtk.Label("User:")
        self.user_cbox = gtk.combo_box_new_text()
        self.users = list()
        add_button_fixed = gtk.Fixed()
        add_button = gtk.Button("Edit")
        add_button.set_size_request(50,35)
        add_button_fixed.put(add_button,50,35)
        add_button.connect("clicked", self.on_click, self)
        
        i = 0
        for user in keyring.get_credentials_list(self.server):
            self.users.append((i, user.attributes["user"]))
            self.user_cbox.insert_text(i, user.attributes["user"])
            i = i + 1
            
        f = gtk.Fixed()
        f.put(self.user_cbox, 0, 0)
        self.user_cbox.set_active(0)
        
        table.attach(self.hostname_label, 0, 1, 0, 1, gtk.FILL)
        table.attach(self.hostname_entry, 1, 2, 0, 1, gtk.FILL)
        table.attach(self.cname_label, 0, 1, 1, 2, gtk.FILL)
        table.attach(self.cname_entry, 1, 2, 1, 2, gtk.FILL)
        table.attach(self.user_label, 0, 1, 2, 3, gtk.FILL)
        table.attach(f, 1, 2, 2, 3, gtk.FILL)
        table.attach(add_button_fixed, 0,1,3,4, gtk.FILL)
        vbox.pack_start(table, False, False, 0)

        self.create_columns(treeView)
        self.statusbar = gtk.Statusbar()

        vbox.pack_start(self.statusbar, False, False, 0)
        
        self.add(vbox)
        
        self.show_all()     
    
    def on_activated(self, widget, row, col):
        model = widget.get_model()
      	
        server = self.serverdb.lookup(model[row][1])
        
        self.hostname_entry.set_text(server.get_hostname())
        self.cname_entry.set_text(server.get_common_name())
        self.edit_server = server
        for user in self.users:
            if user[1] == server.default_user:
            	self.user_cbox.set_active(user[0])
            
        text = model[row][0] + "\n " + model[row][1] + ", " + model[row][2]
        self.statusbar.push(0, text)
        
    def create_model(self):
        store = gtk.ListStore(str,str,str)
        for server in self.serverdb.get_servers():
            store.append([server.get_common_name(), server.get_hostname(), server.get_default_user()])

        return store
	
    def create_columns(self, treeView):
        rendererText = gtk.CellRendererText()
        column = gtk.TreeViewColumn("Common Name", rendererText, text=0)
        column.set_sort_column_id(0)    
        treeView.append_column(column)

        rendererText = gtk.CellRendererText()
        column = gtk.TreeViewColumn("Hostname", rendererText, text=1)
        column.set_sort_column_id(1)
        treeView.append_column(column)

        rendererText = gtk.CellRendererText()
        column = gtk.TreeViewColumn("User", rendererText, text=2)
        column.set_sort_column_id(2)
        treeView.append_column(column)
        
    def on_click(self, widget, test):
        self.edit_server.hostname = self.hostname_entry.get_text()
        self.edit_server.common_name = self.cname_entry.get_text()
        
        for user in self.users:
            if self.user_cbox.get_active() == user[0]:
               self.edit_server.default_user = user[1]

        self.serverdb.write_file(self.config.get("pyrdp", "serverdb"))
        self.destroy()
